package {
	public class TowerControl {
		public function TowerControl() {
			
		}
		
		
		/**
		 * Finds and returns the first creep that is within the given radius from the position provided
		 * 
		 * @access public
		 * @param {int} posX the X coordinate
		 * @param {int} posY the Y coordinate
		 * @param {int} radius the range to scan
		 * @return Creep
		 */
		public function obtainTarget(posX :int, posY :int, radius :int) :Creep
		{
			targetLoop: for (var i :int = 0; i < this.creeps.length; i++)
			{
				// Calculate distance to creep
				var diffX :int = Math.abs(posX - this.creeps[i].x);
				var diffY :int = Math.abs(posY - this.creeps[i].y);
				var dist :int = Math.round(Math.sqrt((diffX * diffX) + (diffY * diffY)));
				
				if (dist <= radius && !this.creeps[i].isDestroyed())
				{
					// return this creep
					return this.creeps[i];
				}
			}
			throw new Error('No targets available');
		}
	}
}